//
//  UITableViewCell+CustomSeparatorStyle.m
//  common
//
//  Created by HuangZuShu on 17/5/2.
//  Copyright © 2017年 Jane. All rights reserved.
//

#import "UITableViewCell+CustomSeparatorStyle.h"



static char kTopLine;
static char kBottomLine;

static char kTopLeftConstraint;
static char kTopHeightConstraint;

static char kBottomLeftConstraint;
static char kBottomHeightConstraint;

@interface UITableViewCell ()

@property(weak, nonatomic) UIView * topLine;
@property(weak, nonatomic) UIView * bottomLine;


@property(weak, nonatomic) NSLayoutConstraint * topLeftConstraint;
@property(weak, nonatomic) NSLayoutConstraint * topHeightConstraint;

@property(weak, nonatomic) NSLayoutConstraint * bottomLeftConstraint;
@property(weak, nonatomic) NSLayoutConstraint * bottomHeightConstraint;


@end

@implementation UITableViewCell (CustomSeparatorStyle)

- (void)separatorStyleWithNumberOfRowsInSection:(NSInteger)rowCount currentRow:(NSInteger)row separatorColor:(UIColor *)separatorColor separatorHeight:(CGFloat)separatorHeight {
    if (rowCount < 1 || row >= rowCount) {
        return;
    }
    if (self.topLine == nil) {
        UIView * topLine = [[UIView alloc] init];
        self.topLine = topLine;
        [self.contentView addSubview:self.topLine];
        self.topLine.translatesAutoresizingMaskIntoConstraints = NO;
        NSLayoutConstraint * tL = [NSLayoutConstraint constraintWithItem:self.topLine
                                                               attribute:NSLayoutAttributeLeft
                                                               relatedBy:NSLayoutRelationEqual
                                                                  toItem:self.contentView
                                                               attribute:NSLayoutAttributeLeft
                                                              multiplier:1
                                                                constant:[UIScreen mainScreen].bounds.size.width];
        self.topLeftConstraint = tL;
        NSLayoutConstraint * tT = [NSLayoutConstraint constraintWithItem:self.topLine
                                                               attribute:NSLayoutAttributeTop
                                                               relatedBy:NSLayoutRelationEqual
                                                                  toItem:self.contentView
                                                               attribute:NSLayoutAttributeTop
                                                              multiplier:1
                                                                constant:0];
        NSLayoutConstraint * tR = [NSLayoutConstraint constraintWithItem:self.topLine
                                                               attribute:NSLayoutAttributeRight
                                                               relatedBy:NSLayoutRelationEqual
                                                                  toItem:self.contentView
                                                               attribute:NSLayoutAttributeRight
                                                              multiplier:1
                                                                constant:0];
        NSLayoutConstraint * tH = [NSLayoutConstraint constraintWithItem:self.topLine
                                                               attribute:NSLayoutAttributeHeight
                                                               relatedBy:NSLayoutRelationEqual
                                                                  toItem:nil
                                                               attribute:NSLayoutAttributeHeight
                                                              multiplier:1
                                                                constant:1];
        self.topHeightConstraint = tH;
        [self.contentView addConstraints:@[tL, tT, tR]];
        [self.topLine addConstraint:tH];
    }
    if (self.bottomLine == nil) {
        UIView * bottomLine = [[UIView alloc] init];
        self.bottomLine = bottomLine;
        
        [self.contentView addSubview:self.bottomLine];
        self.bottomLine.translatesAutoresizingMaskIntoConstraints = NO;
        NSLayoutConstraint * bL = [NSLayoutConstraint constraintWithItem:self.bottomLine
                                                               attribute:NSLayoutAttributeLeft
                                                               relatedBy:NSLayoutRelationEqual
                                                                  toItem:self.contentView
                                                               attribute:NSLayoutAttributeLeft
                                                              multiplier:1
                                                                constant:[UIScreen mainScreen].bounds.size.width];
        self.bottomLeftConstraint = bL;
        NSLayoutConstraint * bB = [NSLayoutConstraint constraintWithItem:self.bottomLine
                                                               attribute:NSLayoutAttributeBottom
                                                               relatedBy:NSLayoutRelationEqual
                                                                  toItem:self.contentView
                                                               attribute:NSLayoutAttributeBottom
                                                              multiplier:1
                                                                constant:0];
        NSLayoutConstraint * bR = [NSLayoutConstraint constraintWithItem:self.bottomLine
                                                               attribute:NSLayoutAttributeRight
                                                               relatedBy:NSLayoutRelationEqual
                                                                  toItem:self.contentView
                                                               attribute:NSLayoutAttributeRight
                                                              multiplier:1
                                                                constant:0];
        NSLayoutConstraint * bH = [NSLayoutConstraint constraintWithItem:self.bottomLine
                                                               attribute:NSLayoutAttributeHeight
                                                               relatedBy:NSLayoutRelationEqual
                                                                  toItem:nil
                                                               attribute:NSLayoutAttributeHeight
                                                              multiplier:1
                                                                constant:1];
        self.bottomHeightConstraint = bH;
        [self.contentView addConstraints:@[bL, bB, bR]];
        [self.bottomLine addConstraint:bH];
        
    }
    CGFloat topLineLeftConstant = [UIScreen mainScreen].bounds.size.width;
    CGFloat bottomLineleftConstant = [UIScreen mainScreen].bounds.size.width;
    if (rowCount == 1) {
        topLineLeftConstant = 0;
        bottomLineleftConstant = 0;
    } else if (row == 0) {
        topLineLeftConstant = 0;
        bottomLineleftConstant = 15;
    } else if (rowCount - 1 == row) {
        topLineLeftConstant = [UIScreen mainScreen].bounds.size.width;
        bottomLineleftConstant = 0;
    } else {
        topLineLeftConstant = [UIScreen mainScreen].bounds.size.width;
        bottomLineleftConstant = 15;
    }
    
    self.topLine.backgroundColor = separatorColor;
    self.bottomLine.backgroundColor = separatorColor;
    
    self.topLeftConstraint.constant = topLineLeftConstant;
    self.topHeightConstraint.constant = separatorHeight;
    
    self.bottomLeftConstraint.constant = bottomLineleftConstant;
    self.bottomHeightConstraint.constant = separatorHeight;
}

- (void)separatorFullWithNumberOfRowsInSection:(NSInteger)rowCount currentRow:(NSInteger)row separatorColor:(UIColor *)separatorColor separatorHeight:(CGFloat)separatorHeight {


    if (rowCount < 1 || row >= rowCount) {
        return;
    }
    if (self.topLine == nil) {
        UIView * topLine = [[UIView alloc] init];
        self.topLine = topLine;
        [self.contentView addSubview:self.topLine];
        self.topLine.translatesAutoresizingMaskIntoConstraints = NO;
        NSLayoutConstraint * tL = [NSLayoutConstraint constraintWithItem:self.topLine
                                                               attribute:NSLayoutAttributeLeft
                                                               relatedBy:NSLayoutRelationEqual
                                                                  toItem:self.contentView
                                                               attribute:NSLayoutAttributeLeft
                                                              multiplier:1
                                                                constant:[UIScreen mainScreen].bounds.size.width];
        self.topLeftConstraint = tL;
        NSLayoutConstraint * tT = [NSLayoutConstraint constraintWithItem:self.topLine
                                                               attribute:NSLayoutAttributeTop
                                                               relatedBy:NSLayoutRelationEqual
                                                                  toItem:self.contentView
                                                               attribute:NSLayoutAttributeTop
                                                              multiplier:1
                                                                constant:0];
        NSLayoutConstraint * tR = [NSLayoutConstraint constraintWithItem:self.topLine
                                                               attribute:NSLayoutAttributeRight
                                                               relatedBy:NSLayoutRelationEqual
                                                                  toItem:self.contentView
                                                               attribute:NSLayoutAttributeRight
                                                              multiplier:1
                                                                constant:0];
        NSLayoutConstraint * tH = [NSLayoutConstraint constraintWithItem:self.topLine
                                                               attribute:NSLayoutAttributeHeight
                                                               relatedBy:NSLayoutRelationEqual
                                                                  toItem:nil
                                                               attribute:NSLayoutAttributeHeight
                                                              multiplier:1
                                                                constant:1];
        self.topHeightConstraint = tH;
        [self.contentView addConstraints:@[tL, tT, tR]];
        [self.topLine addConstraint:tH];
    }
    if (self.bottomLine == nil) {
        UIView * bottomLine = [[UIView alloc] init];
        self.bottomLine = bottomLine;
        
        [self.contentView addSubview:self.bottomLine];
        self.bottomLine.translatesAutoresizingMaskIntoConstraints = NO;
        NSLayoutConstraint * bL = [NSLayoutConstraint constraintWithItem:self.bottomLine
                                                               attribute:NSLayoutAttributeLeft
                                                               relatedBy:NSLayoutRelationEqual
                                                                  toItem:self.contentView
                                                               attribute:NSLayoutAttributeLeft
                                                              multiplier:1
                                                                constant:[UIScreen mainScreen].bounds.size.width];
        self.bottomLeftConstraint = bL;
        NSLayoutConstraint * bB = [NSLayoutConstraint constraintWithItem:self.bottomLine
                                                               attribute:NSLayoutAttributeBottom
                                                               relatedBy:NSLayoutRelationEqual
                                                                  toItem:self.contentView
                                                               attribute:NSLayoutAttributeBottom
                                                              multiplier:1
                                                                constant:0];
        NSLayoutConstraint * bR = [NSLayoutConstraint constraintWithItem:self.bottomLine
                                                               attribute:NSLayoutAttributeRight
                                                               relatedBy:NSLayoutRelationEqual
                                                                  toItem:self.contentView
                                                               attribute:NSLayoutAttributeRight
                                                              multiplier:1
                                                                constant:0];
        NSLayoutConstraint * bH = [NSLayoutConstraint constraintWithItem:self.bottomLine
                                                               attribute:NSLayoutAttributeHeight
                                                               relatedBy:NSLayoutRelationEqual
                                                                  toItem:nil
                                                               attribute:NSLayoutAttributeHeight
                                                              multiplier:1
                                                                constant:1];
        self.bottomHeightConstraint = bH;
        [self.contentView addConstraints:@[bL, bB, bR]];
        [self.bottomLine addConstraint:bH];
        
    }
    CGFloat topLineLeftConstant = [UIScreen mainScreen].bounds.size.width;
    CGFloat bottomLineleftConstant = [UIScreen mainScreen].bounds.size.width;
    if (rowCount == 1) {
        topLineLeftConstant = 0;
        bottomLineleftConstant = 0;
    } else if (row == 0) {
        topLineLeftConstant = 0;
        bottomLineleftConstant = 0;
    } else if (rowCount - 1 == row) {
        topLineLeftConstant = [UIScreen mainScreen].bounds.size.width;
        bottomLineleftConstant = 0;
    } else {
        topLineLeftConstant = [UIScreen mainScreen].bounds.size.width;
        bottomLineleftConstant = 0;
    }
    
    self.topLine.backgroundColor = separatorColor;
    self.bottomLine.backgroundColor = separatorColor;
    
    self.topLeftConstraint.constant = topLineLeftConstant;
    self.topHeightConstraint.constant = separatorHeight;
    
    self.bottomLeftConstraint.constant = bottomLineleftConstant;
    self.bottomHeightConstraint.constant = separatorHeight;
}




- (void)setTopLine:(UIView *)topLine {
    objc_setAssociatedObject(self, &kTopLine, topLine,OBJC_ASSOCIATION_ASSIGN);
}

- (UIView *)topLine {
    return objc_getAssociatedObject(self, &kTopLine);
}

- (void)setBottomLine:(UIView *)bottomLine {
    objc_setAssociatedObject(self, &kBottomLine, bottomLine,OBJC_ASSOCIATION_ASSIGN);
}

- (UIView *)bottomLine {
    return objc_getAssociatedObject(self, &kBottomLine);
}

- (void)setTopLeftConstraint:(NSLayoutConstraint *)topLeftConstraint {
    objc_setAssociatedObject(self, &kTopLeftConstraint, topLeftConstraint,OBJC_ASSOCIATION_ASSIGN);
}
- (NSLayoutConstraint *)topLeftConstraint {
    return objc_getAssociatedObject(self, &kTopLeftConstraint);
}

- (void)setTopHeightConstraint:(NSLayoutConstraint *)topHeightConstraint {
    objc_setAssociatedObject(self, &kTopHeightConstraint, topHeightConstraint,OBJC_ASSOCIATION_ASSIGN);
}
- (NSLayoutConstraint *)topHeightConstraint {
    return objc_getAssociatedObject(self, &kTopHeightConstraint);
}


- (void)setBottomLeftConstraint:(NSLayoutConstraint *)bottomLeftConstraint {
    objc_setAssociatedObject(self, &kBottomLeftConstraint, bottomLeftConstraint,OBJC_ASSOCIATION_ASSIGN);
}
- (NSLayoutConstraint *)bottomLeftConstraint {
    return objc_getAssociatedObject(self, &kBottomLeftConstraint);
}

- (void)setBottomHeightConstraint:(NSLayoutConstraint *)bottomHeightConstraint {
    objc_setAssociatedObject(self, &kBottomHeightConstraint, bottomHeightConstraint,OBJC_ASSOCIATION_ASSIGN);
}
- (NSLayoutConstraint *)bottomHeightConstraint {
    return objc_getAssociatedObject(self, &kBottomHeightConstraint);
}


@end
